/* 
 * This file contains functions to display the student list and all of its functionality
 */


var done = false;   // stopper for the buffer loader


/*
 * This function displays a waiting message when the students gets loaded
 */
function waitForStudents(number){    
    var string = document.getElementById('students').firstChild; // get the string to modify
    if(done == true) {  // if the flag is set to true then stop running
        return;
    } else if(number == 0) { // if the number is 0 then display one .
        string.innerHTML = 'Henter data.';
        setTimeout(function(){  // call the function again with another number
            waitForStudents(1)
        },1000);
    } else if(number == 1) {    // line above from http://www.makemineatriple.com/2007/10/passing-parameters-to-a-function-called-with-settimeout
        string.innerHTML = 'Henter data..';
        setTimeout(function(){  // same as above
            waitForStudents(2)
        },1000);
    } else if(number == 2) {
        string.innerHTML = 'Henter data...';
        setTimeout(function(){      // same as above
            waitForStudents(0)
        },1000);
    }
}

/*
 * This function display the list of the group members and the list functionality
 */
function displayList() {    
    var grpForm = document.getElementById('yourGroup');    // get the group form
    if(ingrp == false) {                                    // checks if the user is in grp
        var nogrp = document.createElement('h3');           // if not then display a warning
        nogrp.innerHTML = grpName;
        var create = document.createElement('button');      // creates a button to create group
        create.setAttribute("onclick", "createGroupDialog();");
        create.innerHTML = "Opprett gruppe";
        grpForm.appendChild(nogrp);                         // add message and button to form
        grpForm.appendChild(create);
    } else {                                            // if the user is in a grp
        grpForm.firstChild.innerHTML += grpName;        // display the group name
        var change = document.createElement('button');      // create change name button
        var del = document.createElement('button');         // create delete group button
        change.setAttribute("onclick", "updateGroupDialog();");
        change.innerHTML = "Bytt navn";                         // set attributs to buttons
        del.setAttribute("onclick", "deleteGroupDialog();");
        del.innerHTML = "Slett Gruppe";
        grpForm.appendChild(change);                        // add them to group form
        grpForm.appendChild(del);            
        
        for(m in grpMembers) {                          // for each group member
            getUser(grpMembers[m], function(stud) {     // get student info
                setTimeout(function() {},500);            // wait half a second 
                var list = document.createElement('li'); // create a list element
                list.name = stud[0];    // set list name        
                list.setAttribute('class', 'studentlist'); // set class to list(css)
                var img = document.createElement('img'); // create icon with attributs
                img.setAttribute('onclick', 'remove_member("'+stud[2] + ', ' + stud[1]+'","'+stud[0]+'");');  // create onclick event to icon   
                img.src = "LnF/x_21x21.png";
                img.height = "15";                              // add icon and height
                list.appendChild(img); // add icon to list  
                list.innerHTML += '<span>' + stud[2] + ', ' + stud[1] + '</span>'; // add name to list
                grpForm.insertBefore(list, change);         // add list to form
            });
        } 
    }
    if(allStudents.length > 0) {    // if the students are created
        displayStudents();          // just display them
    } else {                        // else get them from tvil
        listAllStudents('HØST 2011','Avdeling for informatikk og medieteknik');    
    }    
}

/*
 * This function displays all the students in the list
 */
function displayStudents() {        
    $('#students').empty(); // remove all from student form   
    
    var form = document.getElementById('students'); // get student form
    var search = document.createElement('input');   // create a search input
    search.type = "search";
    search.setAttribute("id", "searchStudent");     // add attributs
    search.setAttribute("placeholder", "SØK ETTER STUDENT: studentnummer eller fornavn (mellomrom) etternavn");    
    form.appendChild(search);     // add the search input to student form
    
    for (a in allStudents){ // loop trough students        
        var form = document.getElementById('students'); // set form to student form
        var list = document.createElement('li'); // create a list element
        list.name = allStudents[a].get_uId();    // set list name        
        list.setAttribute('class', 'studentlist'); // set class to list(css)
        var check = false;
        if(ingrp == true) {     // if the student is one of the groupmembers then dont display student
            for(g in grpMembers) {                // in student form
                if(grpMembers[g] == allStudents[a].get_uId()) {     
                    check = true;
                    break;
                }
            }
            if(check == false) {    // if student is not one of the groupmembers
                var img = document.createElement('img'); // create icon with attributs
                img.setAttribute('onclick', 'add_member(this);');  // create onclick event to icon
                img.src = "LnF/check_24x20.png";
                img.height = "15";          // add attributs
                list.appendChild(img); // add icon to list    
            }
        }
        if(check == false) {        
            list.innerHTML += '<span>' + allStudents[a].get_name() + '</span>'; // add name to list
            form.appendChild(list);         // add list to form       
        }
    } 
}

